<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY 杭州博也网络科技有限公司
// |-----------------------------------------------------------------------------------
// | Author: 吃草的鱼 <783188184@qq.com>
// | Copyright (c) 2013-2016, http://www.itboye.com. All Rights Reserved.
// |-----------------------------------------------------------------------------------

namespace Home\Controller;
use Admin\Api\OrdersPaycodeApi;
use Admin\Api\WeixinLogApi;
use Shop\Api\GroupApi;
use Shop\Api\GrouporderidApi;
use Think\Controller;
use Shop\Api\CategoryApi;
use Shop\Api\ProductApi;
use Shop\Api\ProductSkuApi;
use Shop\Api\ProductImageApi;
use Weixin\Api\WxuserApi;
use Admin\Api\UserPictureApi;
use Shop\Api\AddressApi;
use Shop\Api\OrdersApi;
use Shop\Api\GroupattendApi;
use Shop\Api\GroupOrderApi;
use Shop\Api\ProductGroupApi;
use Shop\Api\OrdersItemApi;
use Shop\Api\OrdersContactInfoApi;
use Weixin\Api\WxaccountApi;
use Common\Api\WeixinApi;
use Home\Controller\GroupController;

class PAYTestController extends Controller {
    public function __construct() {
        parent::__construct();
        // $this->getConfig();
        $result = apiCall(WxaccountApi::GET_INFO, array(array('id' => 5)));
        if ($result['status'] && is_array($result['info'])) {
            $this->wxaccount = $result['info'];
        } else {
            exit("公众号信息获取失败，请重试！");
        }
    }
    /*
     * 异步结果通知地址
     * */
    public function ajaxinformzhang() {

        ignore_user_abort(TRUE);
        //如果客户端断开连接，不会引起脚本abort
        set_time_limit(0);
        $xml        = $GLOBALS['HTTP_RAW_POST_DATA'];
        $array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        if (empty($array_data['out_trade_no'])) {
            $flag = true;
            echo "<xml>
                    <return_code><![CDATA[FAIL]]></return_code>
                    <return_msg><![CDATA[数据接收失败]]></return_msg>
                 </xml>";
        } else {
            //开启事务
            /*$modal = M();
            $modal->startTrans();*/
            $flag                 = false;
            $map['order_code']    = $array_data['out_trade_no'];//返回订单号
            $save['pay_status']   = 1;
            $save['order_status'] = 3;
            $save['pay_code']     = $array_data['transaction_id'];//微信支付订单号
            $orderres             = apiCall(OrdersApi::SAVE, array($map, $save));
            if ($orderres['status']) {
                //查出用户ID
                $wxmap['openid'] = $array_data['openid'];;
                $orderres = apiCall(WxuserApi::GET_INFO, array($wxmap));
                if ($orderres['status']) {
                    $add['createtime'] = time();
                    $add['uid']        = $orderres['info']['id'];
                    $add['pay_type']   = 2;
                    $add['pay_money']  = $array_data['total_fee'];
                    $add['pay_code']   = $array_data['transaction_id'];//微信支付订单号
                    $orderadd          = apiCall(OrdersPaycodeApi::ADD, array($add));
                    if (!$orderadd['status']) {
                        $flag = true;
                    }
                } else {
                    $flag = true;
                }
            } else {
                $flag = true;
            }
        }
        if ($flag) {
            /*$modal->rollback();*/
            echo "<xml>
                    <return_code><![CDATA[FAIL]]></return_code>
                    <return_msg><![CDATA[数据接收失败]]></return_msg>
                 </xml>";
        } else {
            //$hui['group_sum']=$array_data['attach'];$add=apiCall(GroupApi::ADD,array($hui));//回调测试
            $counttype   = $array_data['attach'];//返回订单号
            $order_code = $array_data['out_trade_no'];
            //根据支付编号查出商品和购买者信息
            $map['order_code'] = $order_code;
            //查出商品id
            $itemdata  = apiCall(OrdersItemApi::GET_INFO, array($map));
            $orderdata = apiCall(OrdersApi::GET_INFO, array($map));
            if (!$orderdata['status']) $flag = true;
            if (empty($itemdata['info']))$flag = true;
            if (empty($orderdata['info'])) $flag = true;
            $data             = $itemdata['info'];
            $pid              = $data['p_id'];
            $groupmap['p_id'] = $pid;
            $uid              = $orderdata['info']['uid'];



            //查出商品信息
            $group = apiCall(ProductGroupApi::GET_INFO, array($groupmap));
            if (!$group['status']) $flag = true;
            if (empty($group['info'])) $flag = true;
            $vii['ordercode']=$order_code;
            $vi=apiCall(GroupattendApi::GET_INFO,array($vii));
            if(empty($vi['info'])){
                $con['p_id']=$pid;$con['type']=$counttype;
                $config=apiCall(GroupOrderApi::GET_INFO,array($con));
                $sum    = $config['info']['sum'];
                $entity = ['group_sum' => $sum, 'productid' => $pid, 'group_count' => 1, 'createtime' => time(), 'status' => 1, 'uid' => $uid,'type'=>$counttype];
                $add = apiCall(GroupApi::ADD, array($entity));
                if (empty($add['status'])) $flag = true;
                //调用参团（团长）
                $groupid = $add['info'];
                $uid     = $orderdata['info']['uid'];
                $attend  = array('groupid' => $groupid, 'uid' => $uid, 'status' => 1, 'p_id' => $pid, 'ordercode' => $order_code, 'createtime' => time());
                $addl    = apiCall(GroupattendApi::ADD, array($attend));

                //判断团是否已满
                $id['id']              = $groupid;
                $sum = apiCall(GroupApi::GET_INFO, array($id));
                $sum = $sum['info']['group_sum'];
                //查出参团的人数
                $map                   = [];
                $map['groupid']        = $groupid;
                $count                 = apiCall(GroupattendApi::GET_COUNT, array($map));
                if ($sum <= $count['info']) {
                    $id['id']         = $groupid;
                    $entity           = [];
                    $entity['status'] = 2;
                    $gengxin          = apiCall(GroupApi::SAVE, array($id, $entity));
                    if (!$gengxin['status']) $flag = true;
                    if (empty($gengxin['info'])) $flag = true;
                }



                if (!$addl['status']) $flag = true;
                if ($add['status'] && !empty($add['info'])) {
                    echo "<xml>
                      <return_code><![CDATA[SUCCESS]]></return_code>
                      <return_msg><![CDATA[OK]]></return_msg>
                  </xml>";
                } elseif ($flag) {
                    echo "<xml>
                      <return_code><![CDATA[FAIL]]></return_code>
                      <return_msg><![CDATA[拼团失败]]></return_msg>
                  </xml>";
                } else {
                    echo "<xml>
                      <return_code><![CDATA[FAIL]]></return_code>
                      <return_msg><![CDATA[拼团失败]]></return_msg>
                  </xml>";
                }

            }else{
                    echo "<xml>
                      <return_code><![CDATA[FAIL]]></return_code>
                      <return_msg><![CDATA[拼团失败]]></return_msg>
                  </xml>";
                }
        }
    }


    /*
     * 异步结果通知地址
     * */
    public function ajaxinformyuan() {

        ignore_user_abort(TRUE);
        //如果客户端断开连接，不会引起脚本abort
        set_time_limit(0);
        $xml        = $GLOBALS['HTTP_RAW_POST_DATA'];
        $array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        if (empty($array_data['out_trade_no'])) {
            echo "<xml>
                    <return_code><![CDATA[FAIL]]></return_code>
                    <return_msg><![CDATA[数据接收失败]]></return_msg>
                 </xml>";
        } else {
            //开启事务
            /*$modal = M();
            $modal->startTrans();*/

            $flag                 = false;
            $map['order_code']    = $array_data['out_trade_no'];//返回订单号
            $save['pay_status']   = 1;
            $save['order_status'] = 3;
            $save['pay_code']     = $array_data['transaction_id'];//微信支付订单号
            $orderres             = apiCall(OrdersApi::SAVE, array($map, $save));
            if ($orderres['status']) {
                //查出用户ID
                $wxmap['openid'] = $array_data['openid'];;
                $orderres = apiCall(WxuserApi::GET_INFO, array($wxmap));
                if ($orderres['status']) {
                    $add['createtime'] = time();
                    $add['uid']        = $orderres['info']['id'];
                    $add['pay_type']   = 2;
                    $add['pay_money']  = $array_data['total_fee'];
                    $add['pay_code']   = $array_data['transaction_id'];//微信支付订单号
                    $orderadd          = apiCall(OrdersPaycodeApi::ADD, array($add));
                    if (!$orderadd['status']) {
                        $flag = true;
                    }
                } else {
                    $flag = true;
                }

            } else {
                $flag = true;
            }
        }
        if ($flag) {
            /*$modal->rollback();*/
            echo "<xml>
                    <return_code><![CDATA[FAIL]]></return_code>
                    <return_msg><![CDATA[数据接收失败]]></return_msg>
                 </xml>";
        } else {
            $order_code        = $array_data['out_trade_no'];
            $map['order_code'] = $order_code;
            //查出groupid
            $ma['ordercode'] = $order_code;
            $groupid         = apiCall(GrouporderidApi::GET_INFO, array($ma));
            $groupid         = $groupid['info']['groupid'];
            //查出商品id
            $itemdata  = apiCall(OrdersItemApi::GET_INFO, array($map));
            $orderdata = apiCall(OrdersApi::GET_INFO, array($map));
            if (!$itemdata['status']) $flag = true;
            if ( !$orderdata['status']) $flag = true;
            if (empty($itemdata['info'])) $flag = true;
            if (empty($orderdata['info'])) $flag = true;
            $data = $itemdata['info'];
            $pid  = $data['p_id'];

            $vii['ordercode']=$order_code;
            $vi=apiCall(GroupattendApi::GET_INFO,array($vii));
            if(empty($vi['info'])){
                $counttype   = $array_data['attach'];//返回订单号
            //调用参团（团员）
            $uid    = $orderdata['info']['uid'];
            $attend = array('groupid' => $groupid, 'uid' => $uid, 'status' => 2, 'createtime' => time(), 'p_id' => $pid, 'ordercode' => $order_code);
            $add    = apiCall(GroupattendApi::ADD, array($attend));
            if (!$add['status']) $flag = true;
            if (empty($add['info'])) $flag = true;
            //更新团的参加人数
            $map                   = [];
            $map['groupid']        = $groupid;
            $count                 = apiCall(GroupattendApi::GET_COUNT, array($map));
            $id['id']              = $groupid;
            $entity                = [];
            $entity['group_count'] = $count['info'];
            $gengxin               = apiCall(GroupApi::SAVE, array($id, $entity));
            if (empty($gengxin['info'])) $flag = true;
            //判断团是否已满
            $sum = apiCall(GroupApi::GET_INFO, array($id));
            $sum = $sum['info']['group_sum'];
            if ($sum <= $count['info']) {
                $id['id']         = $groupid;
                $entity           = [];
                $entity['status'] = 2;
                $gengxin          = apiCall(GroupApi::SAVE, array($id, $entity));
                if (!$gengxin['status']) $flag = true;
                if (empty($gengxin['info'])) $flag = true;
            }


            if ($flag) {
                echo "<xml>
                      <return_code><![CDATA[FAIL]]></return_code>
                      <return_msg><![CDATA[拼团失败]]></return_msg>
                  </xml>";
            } else {
                echo "<xml>
                      <return_code><![CDATA[SUCCESS]]></return_code>
                      <return_msg><![CDATA[OK]]></return_msg>
                  </xml>";
            }

            } else {
                echo "<xml>
                      <return_code><![CDATA[SUCCESS]]></return_code>
                      <return_msg><![CDATA[OK]]></return_msg>
                  </xml>";
            }
        }


    }
}

